from networking.ganet.network import message


class Role():
    ATTACKER = 1
    DEFENDER = 2


class Player():
    def __init__(self, peer):
        self.name = peer.addr[1]
        self.peer = peer

    def init_session(self, session, role):
        self.money = 0
        self.role = 0
        self.manager = 0
        self.session = session
        if role == Role.ATTACKER:
            self._init_attacker()
        if role == Role.DEFENDER:
            self._init_defender()
        self.update_money()

    def set_target(self, target):
        self.target = target

    def _init_attacker(self):
        self.money = 200
        self.role = Role.ATTACKER

    def _init_defender(self):
        self.money = 100
        self.role = Role.DEFENDER

    def update(self, dt):
        return

    def phase_prepare(self):
        enc = self._prep_msg()
        enc.write_string("phase_prepare")
        self.send_message(enc.get_message())

    def phase_fight(self):
        enc = self._prep_msg()
        enc.write_string("phase_fight")
        self.send_message(enc.get_message())

    def send_message(self, msg):
        self.peer.send_reliable_sequenced(msg)

    def _prep_msg(self):
        enc = message.MessageEncoder()
        enc.reset()
        #enc.write_string("client")
        #enc.write_int(self.role)
        return enc

    def place_unit(self, dec):
        unit = dec.read_string()
        pos = (dec.read_float(), dec.read_float())
        self._place_unit(unit, pos)

    def place_warpzone(self, dec):
        pos = (dec.read_float(), dec.read_float())
        self._place_warpzone(pos)

    def place_tower(self, dec):
        unit = dec.read_string()
        pos = (dec.read_float(), dec.read_float())
        self._place_tower(unit, pos)

    def upgrade(self, dec):
        ind = dec.read_int()
        upg = dec.read_int()
        self._upgrade(ind, upg)

    def select(self, dec):
        print "sleeeeeeeeect meeee"
        pos = (dec.read_float(),dec.read_float())
        tar = self.manager.select(pos)
        if tar is not None:
            enc = self._prep_msg()
            enc.write_string("select")
            enc.write_int(self.manager.get_placed().index(tar))
            self.send_message(enc.get_message())

    def encode_list(self, enc):
        ulist = self.manager.get_placed()
        enc.write_int(len(ulist))
        for u in ulist:
            enc.write_string(u.name)
            enc.write_float(u.pos.x)
            enc.write_float(u.pos.y)
        return enc

    def update_placed(self):
        enc = self._prep_msg()
        if self.role == Role.DEFENDER:
            enc.write_string("update_towers")
        else:
            enc.write_string("update_units")
        enc = self.encode_list(enc)
        self.send_message(enc.get_message())

    def update_money(self):
        enc = self._prep_msg()
        enc.write_string("update_money")
        enc.write_int(self.money)
        self.send_message(enc.get_message())

    def _upgrade(self, ind, upg):
        if self.manager.upgrade(ind, upg):
            enc = self._prep_msg()
            enc.write_string("upgrade")
            enc.write_int(ind)
            enc.write_int(upg)
            self.send_message(enc.get_message())
            self.session.broadcast_unitposition()
            self.update_money()

    def _place_unit(self, name, pos):
        if self.manager.place_unit(name, pos):
            enc = self._prep_msg()
            enc.write_string("place_unit")
            enc.write_string(name)
            enc.write_float(pos[0])
            enc.write_float(pos[1])
            self.send_message(enc.get_message())
            self.update_money()

    def _place_warpzone(self, pos):
        if self.manager.place_warpzone(pos):
            enc = self._prep_msg()
            enc.write_string("place_warpzone")
            enc.write_float(pos[0])
            enc.write_float(pos[1])
            self.send_message(enc.get_message())
            self.update_money()

    def build_unit(self, name, pos):
        self.manager.build_unit(name, pos)
        enc = self._prep_msg()
        enc.write_string("place_unit")
        enc.write_string(name)
        enc.write_float(pos[0])
        enc.write_float(pos[1])
        self.send_message(enc.get_message())

    def _place_tower(self, name, pos):
        if self.manager.place_tower(name, pos):
            enc = self._prep_msg()
            enc.write_string("place_tower")
            enc.write_string(name)
            enc.write_float(pos[0])
            enc.write_float(pos[1])
            self.send_message(enc.get_message())
            self.update_money()

